package com.zby.charity.utils;

import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    /**
     * 获取指定年月的第一天和最后一天
     *
     * @param year  年份
     * @param month 月份（1-12）
     * @return 包含第一天和最后一天的数组
     */
    public static Date[] getMonthStartAndEndDates(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1); // 月份从0开始
        calendar.set(Calendar.DAY_OF_MONTH, 1); // 当月第一天
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        Date startDate = calendar.getTime(); // 当月第一天

        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);

        Date endDate = calendar.getTime(); // 当月最后一天

        return new Date[]{startDate, endDate};
    }
}
